package io.qar.controller;

import io.qar.commons.log.annotation.LogOperation;
import io.qar.commons.tools.constant.Constant;
import io.qar.commons.tools.page.PageData;
import io.qar.commons.tools.utils.Result;
import io.qar.commons.tools.utils.ExcelUtils;
import io.qar.commons.tools.validator.AssertUtils;
import io.qar.commons.tools.validator.ValidatorUtils;
import io.qar.commons.tools.validator.group.AddGroup;
import io.qar.commons.tools.validator.group.DefaultGroup;
import io.qar.commons.tools.validator.group.UpdateGroup;
import io.qar.config.ftpUtil.FileEntity;
import io.qar.dto.CfgFtpSettingDTO;
import io.qar.dto.FtpParamDTO;
import io.qar.dto.FtpToFtpDTO;
import io.qar.excel.CfgFtpSettingExcel;
import io.qar.service.CfgFtpSettingService;
import io.qar.service.FtpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* qar文件目标源配置表
*
* @author yyyyjinying 
* @since 3.0 2023-10-27
*/
@RestController
@RequestMapping("cfgftpsetting")
@Api(tags="qar文件目标源配置表")
public class CfgFtpSettingController {
    @Autowired
    private CfgFtpSettingService cfgFtpSettingService;

    @Autowired
    private FtpService ftpService;


    /**
     * 获取ftp文件
     * @return
     */
    @ApiOperation("获取ftp的qar文件")
    @PostMapping("ftp/list")
    public Result<List<FileEntity>> ftpList(@RequestBody FtpParamDTO ftpParamDTO){
        //效验数据
        ValidatorUtils.validateEntity(ftpParamDTO);

        List<FileEntity> fileEntityList = ftpService.getFileEntityList(ftpParamDTO);
        return new Result<List<FileEntity>>().ok(fileEntityList);
    }

    /**
     * 获取ftp指定树形子节点文件
     * @return
     */
    @ApiOperation("获取ftp的子节点文件")
    @PostMapping("ftp/{id}")
    public Result<FileEntity> getFileEntityById(@PathVariable("id") String id, @RequestBody FtpParamDTO ftpParamDTO){
        ValidatorUtils.validateEntity(ftpParamDTO);
        AssertUtils.isBlank(id, "id");

        FileEntity fileEntityById = ftpService.getFileEntityById(ftpParamDTO,id);
        return new Result<FileEntity>().ok(fileEntityById);
    }

    /**
     * ftp上传到ftp
     * @return
     */
    @ApiOperation("获取ftp的qar文件")
    @PostMapping("ftp/upload/toftp")
    public Result<Boolean> ftpUploadToFtp(@RequestBody FtpToFtpDTO ftpToFtpDTO){
        ValidatorUtils.validateEntity(ftpToFtpDTO);
        boolean b = ftpService.ftpToFtpUpload(ftpToFtpDTO);
        return new Result<Boolean>().ok(b);
    }

    @ApiOperation("ftp任务")
    @GetMapping("ftp/upload/task/{cfgId}")
    public Result<Boolean> ftpToFtpDataTask(@PathVariable("cfgId") long cfgId){
        AssertUtils.isBlank(String.valueOf(cfgId), "cfgId");
        boolean b = ftpService.ftpToFtpTask(cfgId);
        return new Result<Boolean>().ok(b);
    }



    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @PreAuthorize("hasAuthority('sys:cfgftpsetting:page')")
    public Result<PageData<CfgFtpSettingDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CfgFtpSettingDTO> page = cfgFtpSettingService.page(params);

        return new Result<PageData<CfgFtpSettingDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @PreAuthorize("hasAuthority('sys:cfgftpsetting:info')")
    public Result<CfgFtpSettingDTO> get(@PathVariable("id") Long id){
        CfgFtpSettingDTO data = cfgFtpSettingService.get(id);

        return new Result<CfgFtpSettingDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @PreAuthorize("hasAuthority('sys:cfgftpsetting:save')")
    public Result save(@RequestBody CfgFtpSettingDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        cfgFtpSettingService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @PreAuthorize("hasAuthority('sys:cfgftpsetting:update')")
    public Result update(@RequestBody CfgFtpSettingDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        cfgFtpSettingService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @PreAuthorize("hasAuthority('sys:cfgftpsetting:delete')")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        cfgFtpSettingService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @PreAuthorize("hasAuthority('sys:cfgftpsetting:export')")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CfgFtpSettingDTO> list = cfgFtpSettingService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "qar文件目标源配置表", list, CfgFtpSettingExcel.class);
    }

}